﻿#ifndef PST_POLARCOORDINATEGRAPHWINDOW_H
#define PST_POLARCOORDINATEGRAPHWINDOW_H

#include <QList>
#include "Graph2DWindowBase.h"
#include "DataModelPost.h"
#include "PolarAxisAngular.h"

namespace pst
{
    class PolarCoordinateGraphSidebarSettings;
    class PolarCoordinateGraphSettingDialog;
    class PolarCoordinateSettingData;
    class PolarGraph;
    class LineChartTracer;
    class PolarTracerTableWidget;
    class PolarMeasureTableWidget;

    class PolarCoordinateGraphCenterSettingData : public QObject
    {
    public:
        PolarCoordinateGraphCenterSettingData(QObject* parent) : QObject(parent) {}

        PolarCoordinateSettingData* m_polarCoordinateSettingData = nullptr;
        Style2D_DataProperty* m_style2D_DataProperty = nullptr;
    };

    /// @brief 二维图形
    class PolarCoordinateGraphWindow : public Graph2DWindowBase
    {
        Q_OBJECT
    public:
        explicit PolarCoordinateGraphWindow(QWidget* parent, int windowId);
        ~PolarCoordinateGraphWindow();

        void adjustLegendColumns();

        /// @brief 文件写出至工程文件
        QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false) override;

        /// @brief 从工程文件读入数据
        void readFromProjectFile(QDomElement* e, bool isdiso = false) override;

        void setWindowID(int windowID) override;
        int getWindowID() const override;

        void setWindowUniversalID(int windowUniversalID) override;
        int getWindowUniversalID() const override;

        PolarCoordinateGraphCenterSettingData* getPolarCoordinateGraphCenterSettingData() { return m_polarCoordinateGraphCenterSettingData; };

        void addGraph2D(Info2DPointer& info); // 传引用，改数据

        bool GetRadialAutoFlag() const
        {
            return m_autoRadialRescaleAxes;
        }
        void SetRadialAutoFlag(bool autoRadialRescaleAxes)
        {
            m_autoRadialRescaleAxes = autoRadialRescaleAxes;
        }

        void measureData(MeasureType type);

    public slots:
        // 自适应
        void slot_adaptiveRange() override;
        // 放大
        void slot_amplify() override;
        // 缩小
        void slot_minification() override;
        void slot_chosenItemsChanged(const QList<QStandardItem*>& itemList) override;
        void slot_PolarLinePropertyShow(PolarGraph* plottable, int index);
        void slot_ChangePolarLineProperty(int windowUniversalID, int curveInfoID, Info2DPointer info = nullptr, bool bCheckRename = true);
        void slot_showHideItemActor(QStandardItem* item);

        void slot_updateSelectedGraph();
        void slot_removeSelectedGraph();
        void slot_copySelectedGraph();
        void slot_removeAlldGraph();
        void slot_renameGraph();
        void slot_renameCheck(QString name);

        void slot_AddTracer();
        void slot_ShowPreviewTracer(QMouseEvent* event);
        void slot_removeSelectedTracer(const QList<LineChartTracer*>& selectTracers);
        void slot_removeSelectedTracer_script(const QList<LineChartTracer*>& selectTracers);
        void slot_removeAllTracer(const QList<LineChartTracer*>& tracers);
        void slot_removeAllTracer_script(const QList<LineChartTracer*>& tracers);
        void slot_exportMarkerData();

        void slot_TracerPropertyShow(LineChartTracer* tracer);
        void slot_ChangeTracerProperty(int tracerID, int guideLineID);

        void slot_updateBeamBandwidthProperty();

        void slot_getSidebarCurveDataSource(QString& dataSource);

        void slot_updateSelectedGraphState();

        void slot_updateName();
        QString slot_getCurveName(QStandardItem* item, const Info2DPointer& info);

        void slot_changeAllCurveVariableAxis(const QString& newVariableAxisName);

    protected slots:
        // item随graph的选中状态改变而改变
        void slot_polarGraphSelectionChanged(bool selected);

        //左侧关闭工程树，删除相应的图像
        // 注意items是左侧工程树的节点
        void slot_closeTree(const QList<QStandardItem*>& items);

    public: // Python
        void pyVariableAxisChanged(QList<int> allSelectedItemIndexList, int result, QString variableAxistCurrentText);
        void pyAllVariableAxisChanged(QString variableAxistCurrentText);
        void pyCurveNameClicked(QList<int> allSelectedItemIndexList);
        void pyCurveNameCheckStateChanged(int index, int checkStatus);
        void pyDataOperationChanged(bool isNormChecked, bool isDBChecked);
        void pyDeleteAllGraph();
        void pyRenameSelectedGraph(QString name);
        void pyComplexPartsChanged(bool isAmplitudeChecked, bool isPhaseChecked, bool isRealPartChecked, bool isImaginaryPartChecked);
        void pyFixedValueChanged(QList<int> indexList);
        void pyRemoveSelectedGraph(QList<int> indexList);
        void pyCopySelectedGraph();
        void pyDataOperationChanged(int currentIndex);
        void slot_polarCoordinateGraphSaveMainSettingData(int windowUniversalID, int index, QString angDisAxisColor, int angDisAxisThickness, double angDisValuePrecision, QString angDisAxisDigitColor, int angDisAxisDigitSize,
                                                          bool angRangIsAutoStepSize, double angRangCustomStepSize,
                                                          QString radDisAxisColorStr, int radDisAxisThickness, double radDisValuePrecision, QString radDisAxisDigitColorStr, int radDisAxisDigitSize,
                                                          bool radRangIsAutoRange, double radRangMaxValue, double radRangMinValue, bool radRangIsAutoStepSize, double radRangCustomStepSize,
                                                          bool gridIsMainGridLineDisplay, QString gridMainGridLineColor, int gridMainGridLineStyle, double gridMainGridLineThickness,
                                                          QString titleName, QString titleFontColor, int titleFontSize,
                                                          QString legendFontColor, int legendFontSize, QString legendLineColor, int legendLineThickness,
                                                          QString dataProResultDataName, QString dataProDataLineColor, int dataProDataLineStyle, double dataProDataLineThickness, bool dataProIsDisplaySymbol, int dataProSymbolStyle, double dataProSymbolSize, QString dataProSymbolColor);
        void pyAddTracer(QString name, double angle, int tracerStyle, int size, int penR, int penG, int penB, int penA,
                        int brushR, int brushG, int brushB, int brushA, int interpolating, int curveInfoID, int tracerIndex);
        void pyRemoveSelectedTracer(QList<int> selectTracersCurveInfoID, int curveInfoID);
        void pyRemoveAllTracer(QList<int> selectTracersCurveInfoID);
        void pyExportMarkerData(QString fileName);
        void pyChangeTracerProperty(QString name, double x, int symbolStyle, int size, int penR, int penG, int penB, int penA, int tracerID);

    protected:
        void initConnections() override;
        void updateCurve(QStandardItem* item, Info2DPointer& info) override;
        bool eventFilter(QObject* watched, QEvent* event) override;

        QStandardItem* copyGraph(Info2DPointer& info, const QString& curveName);
        QStandardItem* addNewGraph(
            Info2DPointer& info, const QString& name);
        QStandardItem* addNewGraphByOpenProject(Info2DPointer& info, const QString& name);// 打开项目时添加曲线

        void removeSelectedGraph(const QList<Info2DPointer> &selectedGraphs);

    private:
        void setTracerTableWidgetVisible(QTableWidget* widget, bool visible);
        void InitializeResultWindowStyle();
        PolarCoordinateGraphCenterSettingData* m_polarCoordinateGraphCenterSettingData;
        PolarCoordinateSettingData* m_polarCoordinateSetting;
        /// @brief 二维图形设置
        PolarCoordinateGraphSidebarSettings* m_settings{nullptr};
        PolarAxisAngular* m_angularAxis{nullptr};

        bool m_autoRadialRescaleAxes = true;
        PolarGraph* m_doubleClickPlottable = nullptr;
        LineChartTracer* m_doubleClickTracer = nullptr;

        int m_graphIndex{0};
        int m_tracerIndex{0};
        PolarTracerTableWidget* m_tracerPropertyWidget;
        PolarMeasureTableWidget* m_measureTableWidget;
        int m_windowUniversalID{-1};
        int m_windowId;
        /*
         * 自增命名
         */
        QString PolarCoordinateGraphWindow::autoIncrementName(QString name);
        QHash<int, QString> getMarkerNameMap();
    };
} // namespace pst

#endif // PST__POLARLINEWINDOW_H
